﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Quản_Lý_Phòng_Khám_____ver_1._0
{
    public partial class FrmListdrugs : Form
    {
        string strconnect = "Data Source=MNHTUAN-PC\\MINHTUAN;Initial Catalog=QLPK;Persist Security Info=True;User ID=sa;Password=1234567890";
            SqlConnection conn = null;
            SqlDataAdapter dathuoc = null;
            DataTable dtthuoc = null;
            bool Them;

        public FrmListdrugs()
        {
            InitializeComponent();
        }

        private void FrmListdrugs_Load(object sender, EventArgs e)
        {
            LoadData();

            // TODO: This line of code loads data into the 'qLPKDataSet2.thuoc' table. You can move, or remove it, as needed.
            this.thuocTableAdapter1.Fill(this.qLPKDataSet2.thuoc);
            // TODO: This line of code loads data into the 'qLPKDataSet1.thuoc' table. You can move, or remove it, as needed.
            this.thuocTableAdapter.Fill(this.qLPKDataSet1.thuoc);

        }
       
        void LoadData()
        {
            try
            {
                conn = new SqlConnection(strconnect);
                dathuoc = new SqlDataAdapter("select * from thuoc",conn);
                dtthuoc = new DataTable();
                dtthuoc.Clear();
                dathuoc.Fill(dtthuoc);
                dgvThuoc.DataSource = dtthuoc;

               // this.txtMathuoc.ResetText();
                this.txtTenthuoc.ResetText();
                this.txttenhoatchat.ResetText();
                this.txthamluong.ResetText();
                this.txtdonvitinh.ResetText();
                this.txtdonvisudung.ResetText();
                this.txtDongia.ResetText();
                this.txtton.ResetText();

                this.btnLuu.Enabled = false;
                this.btnhuybo.Enabled = false;
                this.panel.Enabled = false;

                this.btnThem.Enabled = true;
                this.btnsua.Enabled = true;
                this.btnxoa.Enabled = true;
                this.btntrove.Enabled = true;
            }
            catch (SqlException)
            {
                MessageBox.Show("Không lấy được nội dung trong table Thuoc. Lỗi rồi!!!");
            }
        }

        private void btnReload_Click(object sender, EventArgs e)
        {
            LoadData();
        }

        private void btnThem_Click(object sender, EventArgs e)
        {
            Them = true;
           // this.txtMathuoc.ResetText();
            this.txtTenthuoc.ResetText();
            this.txttenhoatchat.ResetText();
            this.txthamluong.ResetText();
            this.txtdonvitinh.ResetText();
            this.txtdonvisudung.ResetText();
            this.txtDongia.ResetText();
            this.txtton.ResetText();

            this.btnLuu.Enabled = true;
            this.btnhuybo.Enabled = true;
            this.panel.Enabled = true;

            this.btnThem.Enabled = false;
            this.btnsua.Enabled = false;
            this.btnxoa.Enabled = false;
            this.btntrove.Enabled = false;

            //this.txtMathuoc.Focus();
        }

        private void btnsua_Click(object sender, EventArgs e)
        {
            Them = false;
            this.panel.Enabled = true;
            int r = dgvThuoc.CurrentCell.RowIndex;
            //this.txtMathuoc.Text = dgvThuoc.Rows[r].Cells[0].Value.ToString();
            this.txtTenthuoc.Text = dgvThuoc.Rows[r].Cells[1].Value.ToString();
            this.txttenhoatchat.Text = dgvThuoc.Rows[r].Cells[2].Value.ToString();
            this.txtdonvitinh.Text = dgvThuoc.Rows[r].Cells[3].Value.ToString();
            this.txtdonvisudung.Text = dgvThuoc.Rows[r].Cells[4].Value.ToString();
            this.txthamluong.Text = dgvThuoc.Rows[r].Cells[5].Value.ToString();
            this.txtDongia.Text = dgvThuoc.Rows[r].Cells[6].Value.ToString();
            this.txtton.Text = dgvThuoc.Rows[r].Cells[7].Value.ToString();

            this.btnLuu.Enabled = true;
            this.btnhuybo.Enabled = true;
            this.panel.Enabled = true;

            this.btnThem.Enabled = false;
            this.btnsua.Enabled = false;
            this.btnxoa.Enabled = false;
            this.btntrove.Enabled = false;

            //this.txtMathuoc.Focus();
        }

        private void btnLuu_Click(object sender, EventArgs e)
        { 
            conn.Open();
            if (Them)
            {
                try
                {
                    SqlCommand cmd = new SqlCommand("SET IDENTITY_INSERT thuoc ON");
                    cmd.Connection = conn;
                    cmd.CommandType = CommandType.Text;
                    cmd.CommandText = System.String.Concat("insert into Thuoc(tenthuoc,tenhoatchat,hamluong,donvitinh,Donvisudung,DonGia,Ton) values(" + "N'" 
                    //+ this.txtMathuoc.Text.ToString() + "',N'" 
                    + this.txtTenthuoc.Text.ToString() + "',N'" 
                    + this.txttenhoatchat.Text.ToString() + "',N'" 
                    + this.txthamluong.Text.ToString() + "',N'" 
                    + this.txtdonvitinh.Text.ToString() + "',N'" 
                    + this.txtdonvisudung.Text.ToString() + "',N'" 
                    + this.txtDongia.Text.ToString() + "',N'"
                    +  this.txtton.Text.ToString() + "')");
                    cmd.ExecuteNonQuery();
                    LoadData();
                    MessageBox.Show("Đã thêm xong!", "Thông Báo");
                }
                catch (SqlException)
                {
                    MessageBox.Show("Không thêm được.Lỗi rồi!");
                }
            }
            if (!Them)
            {
                try
                {
                    SqlCommand cmd = new SqlCommand("SET IDENTITY_INSERT thuoc ON");
                    cmd.Connection = conn;
                    cmd.CommandType = CommandType.Text;

                    int r = dgvThuoc.CurrentCell.RowIndex;
                    string strMathuoc = dgvThuoc.Rows[r].Cells[0].Value.ToString();
                    cmd.CommandText = System.String.Concat("Update thuoc set tenthuoc=N'" 
                    + this.txtTenthuoc.Text.ToString() + "',tenhoatchat=N'" 
                    + this.txttenhoatchat.Text.ToString() + "',donvitinh=N'"
                    + this.txtdonvitinh.Text.ToString() + "',hamluong=N'"
                    + this.txthamluong.Text.ToString() + "',donvisudung=N'" 
                    + this.txtdonvisudung.Text.ToString() + "',DonGia=N'"
                    + this.txtDongia.Text.ToString() + "',ton=N'" 
                    + this.txtton.Text.ToString() + "'where Mathuoc='" + strMathuoc + "'");

                    cmd.CommandType = CommandType.Text;
                    cmd.ExecuteNonQuery();
                    LoadData();
                    MessageBox.Show("Đã sửa xong", "Thông Báo");
                }

                catch (SqlException)
                {
                    MessageBox.Show("Không thêm được. Lỗi rồi");
                }

            }
          
            conn.Close();
        }

        private void btnhuybo_Click(object sender, EventArgs e)
        {
           // this.txtMathuoc.ResetText();
            this.txtTenthuoc.ResetText();
            this.txttenhoatchat.ResetText();
            this.txthamluong.ResetText();
            this.txtdonvitinh.ResetText();
            this.txtdonvisudung.ResetText();
            this.txtDongia.ResetText();
            this.txtton.ResetText();

            this.btnThem.Enabled = true;
            this.btnsua.Enabled = true;
            this.btnxoa.Enabled = true;
            this.btntrove.Enabled = true;

            this.btnLuu.Enabled = false;
            this.btnhuybo.Enabled = false;
            this.panel.Enabled = false;
        }

        private void btnxoa_Click(object sender, EventArgs e)
        {
            conn.Open();
            
            try 
            {
                DialogResult traloi;
                traloi = MessageBox.Show("Bạn có chắc chắn xóa không?", "Thông Báo", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);  

                if (traloi == DialogResult.OK)
                {
                    SqlCommand cmd = new SqlCommand("SET IDENTITY_INSERT thuoc ON");
                    cmd.Connection = conn;
                    cmd.CommandType = CommandType.Text;
                    int r = dgvThuoc.CurrentCell.RowIndex;
                    string strMaThuoc = dgvThuoc.Rows[r].Cells[0].Value.ToString();
                    cmd.CommandText = System.String.Concat("Delete from thuoc where mathuoc=" + strMaThuoc + "");
                    cmd.CommandType = CommandType.Text;
                    cmd.ExecuteNonQuery();
                    LoadData();
                }
 
            }
            catch (SqlException)
            {
                MessageBox.Show("Không xóa được. Lỗi rồi");   
            }
            conn.Close();
        }

        private void btntrove_Click(object sender, EventArgs e)
        {
            this.Hide();
            FrmChinh Chinh = new FrmChinh();
            Chinh.ShowDialog();
            this.Close();
        }
        protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {
            switch (keyData)
            {
                case (Keys.Control | Keys.R):
                    btnReload_Click(null, null);
                    break;
                case (Keys.Control | Keys.N):
                    btnThem_Click(null, null);
                    break;
                case (Keys.Control | Keys.H):
                    btnhuybo_Click(null, null);
                    break;
                case (Keys.Control | Keys.S):
                    btnLuu_Click(null, null);
                    break;
                case (Keys.Control | Keys.E):
                    btnsua_Click(null, null);
                    break;
                case (Keys.Control | Keys.X):
                    btnxoa_Click(null, null);
                    break;
                case (Keys.Control | Keys.T):
                    btntrove_Click(null, null);
                    break;
            }

            return base.ProcessCmdKey(ref msg, keyData);
        }  
    }
    
}
